package com.thssh.ui.lsn02;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.view.ContextThemeWrapper;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.thssh.ui.R;
import com.thssh.ui.lsn02.flowlayout.view.FlowLayout;
import com.thssh.ui.lsn02.homework.view.FixedListView;
import com.thssh.ui.lsn02.homework.view.WaterfallLayout;
import com.thssh.ui.utils.UiUtils;

import java.util.Random;

/**
 * @author zhangyugehu
 * @version V1.0
 * @date 2017/09/06
 */

public class Lsn02Activity extends AppCompatActivity {

    private FlowLayout flowLayout;
    private WaterfallLayout wl_container;

    private EditText et_add_text;
    private ListView list_view;
    private FixedListView fixed_list_view;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().setTitle(R.string.des_lsn02);
        setContentView(R.layout.activity_lsn02);
        flowLayout = (FlowLayout) findViewById(R.id.flowLayout);
        et_add_text = (EditText) findViewById(R.id.et_add_text);
        wl_container = (WaterfallLayout) findViewById(R.id.wl_container);
        list_view = (ListView) findViewById(R.id.list_view);
        fixed_list_view = (FixedListView) findViewById(R.id.fixed_list_view);
        String[] data = new String[]{"第一行", "第二行", "第三行", "第四行", "最后一行"};
        list_view.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data));
        fixed_list_view.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data));
    }

    public void addTextClick(View view) {
        String addText = et_add_text.getText().toString().trim();
        if(TextUtils.isEmpty(addText)){
            return;
        }

        @SuppressLint("RestrictedApi") ContextThemeWrapper wrappedContext = new ContextThemeWrapper(this, R.style.TextStyle01);
        TextView tv = new TextView(wrappedContext);
        ViewGroup.MarginLayoutParams params = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        int dp5 = UiUtils.dp2px(5);
        params.setMargins(dp5, dp5, dp5, dp5);
        tv.setText(addText);
        flowLayout.addView(tv, params);
    }

    public void addPicClick(View view) {
        ImageView iv = new ImageView(this);
        iv.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        int drawableRid = R.mipmap.img_chou1;
        int index = new Random().nextInt(5);
        switch (index){
            case 0:
                drawableRid = R.mipmap.img_1;
                break;
            case 1:
                drawableRid = R.mipmap.img_2;
                break;
            case 2:
                drawableRid = R.mipmap.img_3;
                break;
            case 3:
                drawableRid = R.mipmap.img_4;
                break;
            case 4:
                drawableRid = R.mipmap.img_5;
                break;
            case 5:
                drawableRid = R.mipmap.img_6;
                break;
            default:
                drawableRid = R.mipmap.img_chou1;
                break;
        }
        iv.setImageResource(drawableRid);
        wl_container.addView(iv, new WaterfallLayout.WaterfallLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    }
}
